package com.nisco.ampf.util;

import java.io.IOException;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

import android.util.Log;

public class WebServerConn
{
	// 指定 WebService 的命名空间和调用方法
	private static final String NAMESPACE = "http://tempuri.org/";
	private static final String LOGIN_METHOD_NAME = "LoginCheck";
	private static final String GETDATASET_METHOD_NAME = "GetDataSet";
	private static String URL = "http://172.18.199.21:809/DBServer.asmx";

	private static String dbType = "DB02";
	SoapObject soapObject = null;
	SoapSerializationEnvelope envelope = null;
	HttpTransportSE ht = null;

	public String Login(String name, String password)
	{
		try
		{
			// 实例化并设置调用方法的参数值
			soapObject = new SoapObject(NAMESPACE, LOGIN_METHOD_NAME);
			Log.i("WebServer", "实例化SoapObject");
			soapObject.addProperty("UserID", name);
			soapObject.addProperty("Pwd", password);
			// 生成调用Webservice方法的SOAP请求信息。版本号需要根据服务端WebService的版本号设置。
			envelope = new SoapSerializationEnvelope(SoapEnvelope.VER12);
			envelope.bodyOut = soapObject;
			envelope.dotNet = true;
			envelope.setOutputSoapObject(soapObject);
			Log.i("WebServer", "生成调用Webservice方法的SOAP请求信息。版本号需要根据服务端WebService的版本号设置。");
			// 创建HttpTransportsSE对象。
			ht = new HttpTransportSE(URL);
			ht.debug = true;
			Log.i("WebServer", "创建HttpTransportsSE对象。");
			// 使用call方法调用WebService方法
			ht.call(NAMESPACE + LOGIN_METHOD_NAME, envelope);
			Log.i("WebServer", "创建HttpTransportsSE对象。");
			// 获得WebService方法的返回结果
			// SoapObject result = (SoapObject) envelope.bodyIn;
			// detail = (SoapObject) result.getProperty("LoginCheckResult");
			Object detail = (Object) envelope.getResponse();
			Log.i("WebServer", detail.toString());
			return detail.toString();
		} catch (IOException e)
		{
			e.printStackTrace();
			return "文件读取错误";
		} catch (XmlPullParserException e)
		{
			e.printStackTrace();
			return "未连接到WebServer！";
		}

	}

	/**
	 * 输入垛位，板坯号查询
	 * 
	 * @param stackLoc
	 * @param slabNo
	 * @return
	 */
	public String[] queryEndoderm(String stackLoc, String slabNo)
	{
		try
		{
			StringBuffer sqlBuffer = new StringBuffer();
			sqlBuffer.append("select Y.YARD_ADDR,Y.BED_SEQ,Y.SLAB_NO,P.PROC_CD,P.APLY_STDSPEC,P.THK,P.WID,P.LEN,P.WGT  ");
			sqlBuffer.append(" from FP_SLABYARD Y ,FP_SLAB P where Y.YARD_KND = '00'  AND Y.SLAB_NO = P.SLAB_NO(+) ");
			sqlBuffer.append("AND Y.SLAB_NO IS NOT NULL ");
			if (!stackLoc.equals(""))
			{
				sqlBuffer.append(" AND y.yard_addr = '" + stackLoc + "'");
			}
			if (!slabNo.equals(""))
			{
				sqlBuffer.append(" AND Y.SLAB_NO='" + slabNo + "'");
			}
			sqlBuffer.append(" order by YARD_ADDR, BED_SEQ desc");
		
			// 实例化并设置调用方法的参数值
			soapObject = new SoapObject(NAMESPACE, GETDATASET_METHOD_NAME);
			soapObject.addProperty("dbType", dbType);
			soapObject.addProperty("cmdtxt", sqlBuffer.toString());
			soapObject.addProperty("tablename", ConstString.TABLE_SLAB);

			// 生成调用Webservice方法的SOAP请求信息。版本号需要根据服务端WebService的版本号设置。
			envelope = new SoapSerializationEnvelope(SoapEnvelope.VER12);
			envelope.bodyOut = soapObject;
			envelope.dotNet = true;
			envelope.setOutputSoapObject(soapObject);
			// 创建HttpTransportsSE对象。
			ht = new HttpTransportSE(URL);
			ht.debug = true;
			// 使用call方法调用WebService方法
			ht.call(NAMESPACE + GETDATASET_METHOD_NAME, envelope);
			// 获得WebService方法的返回结果
			SoapObject result = (SoapObject) envelope.bodyIn;
			Log.i("WebServer", result.toString());
			String s = result.toString();

			int resultlength = s.length();
			s = s.replace(ConstString.TABLE_SLAB, "");
			int lastlength = (resultlength - s.length()) / ConstString.TABLE_SLAB.length();

			// 取得每个GP_PLATEYARD中的值。
			String LoginReturn = "", pattern = "YARD_ADDR=.*?;\\s*BED_SEQ=.*?;\\s*SLAB_NO=.*?;\\s*PROC_CD=.*?;\\s*APLY_STDSPEC=.*?;\\s*THK=.*?;\\s*WID=.*?;\\s*LEN=.*?;\\s*WGT=.*?; ";
			// "THK","WID","LEN","WGT"
			// 动态生成String 数组，存储查询出来的信息
			String[] GetFinalReturn = new String[lastlength];
			for (int i = 0; i < lastlength; i++)
			{
				LoginReturn = s.replaceFirst("^.*(" + pattern + ").*$", "$1");
				GetFinalReturn[i] = LoginReturn;
				Log.i("test", "wj=" + GetFinalReturn[i]);
				s = s.replace(LoginReturn, "");
			}
			if (GetFinalReturn.length > 0)
			{
				return GetFinalReturn;
			} else
			{
				return null;
			}
		} catch (IOException e)
		{
			e.printStackTrace();
			return null;
		} catch (XmlPullParserException e)
		{
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 根据垛位号或钢板号查询
	 * 
	 * @param stackLoc
	 * @param plateNo
	 * @return
	 */
	public String[] queryStack(String stackLoc, String plateNo)
	{
		String[] GetFinalReturn = null;
		try
		{

			StringBuffer cmdtxt = new StringBuffer("select Y.YARD_ADDR,Y.BED_SEQ,Y.PLATE_NO,P.PROC_CD,P.APLY_STDSPEC,P.THK,P.WID,P.LEN,P.WGT  " + "from GP_PLATEYARD Y ,GP_PLATE P where Y.YARD_KND = '00' AND P.PROD_CD ='PP' AND  " + "Y.PLATE_NO = P.PLATE_NO");

			if (!stackLoc.equals(""))
			{
				cmdtxt.append(" AND y.yard_addr = '" + stackLoc + "'");
			}
			if (!plateNo.equals(""))
			{
				cmdtxt.append(" AND Y.PLATE_NO='" + plateNo + "'");
			}
			cmdtxt.append(" order by YARD_ADDR, BED_SEQ desc");

			// 实例化并设置调用方法的参数值
			soapObject = new SoapObject(NAMESPACE, GETDATASET_METHOD_NAME);
			soapObject.addProperty("dbType", dbType);
			soapObject.addProperty("cmdtxt", cmdtxt.toString());
			soapObject.addProperty("tablename", ConstString.TABLE_PLATE);

			// 生成调用Webservice方法的SOAP请求信息。版本号需要根据服务端WebService的版本号设置。
			envelope = new SoapSerializationEnvelope(SoapEnvelope.VER12);
			envelope.bodyOut = soapObject;
			envelope.dotNet = true;
			envelope.setOutputSoapObject(soapObject);
			// 创建HttpTransportsSE对象。
			ht = new HttpTransportSE(URL);
			ht.debug = true;
			// 使用call方法调用WebService方法
			ht.call(NAMESPACE + GETDATASET_METHOD_NAME, envelope);
			// 获得WebService方法的返回结果
			SoapObject result = (SoapObject) envelope.bodyIn;
			Log.i("WebServer", result.toString());
			String s = result.toString();

			int resultlength = s.length();
			s = s.replace(ConstString.TABLE_PLATE, "");
			int lastlength = (resultlength - s.length()) / ConstString.TABLE_PLATE.length();

			// 取得每个GP_PLATEYARD中的值。
			String LoginReturn = "", pattern = "YARD_ADDR=.*?;\\s*BED_SEQ=.*?;\\s*PLATE_NO=.*?;\\s*PROC_CD=.*?;\\s*APLY_STDSPEC=.*?;\\s*THK=.*?;\\s*WID=.*?;\\s*LEN=.*?;\\s*WGT=.*?; ";
			// "THK","WID","LEN","WGT"
			// 动态生成String 数组，存储查询出来的信息
			GetFinalReturn = new String[lastlength];
			for (int i = 0; i < lastlength; i++)
			{
				LoginReturn = s.replaceFirst("^.*(" + pattern + ").*$", "$1");
				GetFinalReturn[i] = LoginReturn;
				Log.i("test", "wj=" + GetFinalReturn[i]);
				s = s.replace(LoginReturn, "");
			}
			if (GetFinalReturn.length > 0)
			{
				return GetFinalReturn;
			} else
			{
				return null;
			}
		} catch (IOException e)
		{
			e.printStackTrace();
		} catch (XmlPullParserException e)
		{
			e.printStackTrace();
		}
		return GetFinalReturn;
	}

	/**
	 * 根据垛号、钢板号、操作人员及新垛号进行倒垛
	 * 
	 * @param tempDate
	 * @param newStackLoc
	 * @param userNo
	 * @return
	 */
	public String changeStack(String tempDate, String newStackLoc, String userNo,String serviceName)
	{
		try
		{
			// String stompData =
			// "P1T0103,11300209030204;P1T0103,11300209030205";//跺号+钢板号
			String stompData = tempDate;

			String new_yardaddr = newStackLoc;// 新剁位
			String user_no = userNo;// 用户

			// 实例化并设置调用方法的参数值
			soapObject = new SoapObject(NAMESPACE, serviceName);
			soapObject.addProperty("dbType", dbType);
			soapObject.addProperty("stompData", stompData);
			soapObject.addProperty("new_yardaddr", new_yardaddr);
			soapObject.addProperty("user_no", user_no);

			// 生成调用Webservice方法的SOAP请求信息。版本号需要根据服务端WebService的版本号设置。
			envelope = new SoapSerializationEnvelope(SoapEnvelope.VER12);
			envelope.bodyOut = soapObject;
			envelope.dotNet = true;
			envelope.setOutputSoapObject(soapObject);
			Log.i("WebServer", "生成调用Webservice方法的SOAP请求信息。版本号需要根据服务端WebService的版本号设置。");
			// 创建HttpTransportsSE对象。
			ht = new HttpTransportSE(URL);
			ht.debug = true;
			Log.i("WebServer", "创建HttpTransportsSE对象。");
			// 使用call方法调用WebService方法
			ht.call(NAMESPACE + serviceName, envelope);
			Log.i("WebServer", "创建HttpTransportsSE对象。");
			// 获得WebService方法的返回结果

			Object result = (Object) envelope.getResponse();
			String s = result.toString();
			return s;
		} catch (IOException e)
		{
			e.printStackTrace();
			return "文件读取错误";
		} catch (XmlPullParserException e)
		{
			e.printStackTrace();
			return "未连接到WebServer！";
		}
	}

}
